
import { _decorator, Component, Node, Sprite, instantiate, Vec2, Label, Canvas, UITransform, Prefab } from 'cc';
import { Bullet } from './bullet/Bullet';
import { BattleMap } from './game/BattleMap';
import { BattleMapGrid } from './game/BattleMapGrid';
import { CollisonManager } from './game/CollisonManager';
import { JoyStick } from './misc/JoyStick';
import { Monster } from './Monster';
import { Player } from './Player';
import { MouseInput } from './Player/MouseInput';
const { ccclass, property } = _decorator;

 
@ccclass('Main')
export class Main extends Component {
	static _i:Main
	static get I():Main{
		return Main._i
	}
	@property({ type: Canvas })
	canvas: Canvas = null
	@property({type:Prefab})
	monsterPrefab: Prefab=null
	@property({type:Node})
	groundNode:Node=null
	@property({type:Player})
	player:Player=null
	@property({type:Label})
	label:Label=null
	@property({type:MouseInput})
	mouseInput:MouseInput=null
	@property({type:JoyStick})
	joyStick:JoyStick=null
	@property({type:BattleMap})
	battleMap:BattleMap=null
	@property({type:Node})
	bulletPlace:Node=null

	@property({type:Label})
	testMessage:Label=null

	
	collisionManager:CollisonManager

	monsters:Monster[]=[]
	bullets:Bullet[]=[]
	
	screenWidth=720
	screenHeight=1334

	private _createCount=0

    start () {
		Main._i=this
		window['$main'] = this
		this.screenWidth=this.canvas.getComponent(UITransform).width
		this.screenHeight=this.canvas.getComponent(UITransform).height
		this.collisionManager=new CollisonManager()

		this.battleMap.init()
		this.mouseInput.init()

		this.createMonster()
    }
	update(){
		this.mouseInput.loop()
		this.joyStick.loop()

		//玩家移动
		this.player.loop()
		//敌人移动
		let _emenys=this.monsters.concat()
		_emenys.forEach(enemy=>{
			enemy.loop()
		})

		
		//生成怪物
		this._createCount++
		if(this._createCount>5){
			this._createCount=0
			this.createMonster()
		}
	

		// //处理碰撞
		this.battleMap.reset(this.player.x,this.player.y)
		this.player.resetGrid()
		this.battleMap.setPlayer(this.player)
		this.monsters.forEach(monster=>{
			monster.resetGrid()
			this.battleMap.setMonster(monster)
		})
		this.battleMap.dealCollision() //计算排开向量
		this.player.useSperateVec() //使用排开向量4
		this.monsters.forEach(monster=>{
			monster.useSperateVec()
		})

		//子弹逻辑
		this.bullets.concat().forEach(bullet=>{
			bullet.loop()
		})

		//清除死亡的敌人
		for(let i=this.monsters.length-1;i>=0;i--){
			let monster=this.monsters[i]
			if(monster.isDie){
				this.monsters.splice(i,1)
				monster.node.destroy()
			}
		}
		//排序
		let children=this.groundNode.children
		children.sort((a,b)=>{
			return b.position.y-a.position.y
		})
		this.label.string=`敌人数量:${this.monsters.length}`
	}
	createMonster(){
		var x=Math.random()*1000-500+this.player.x
		var y=Math.random()*2000-1000+this.player.y
		if(this.battleMap.testHitMonsters(x,y,60,60).length>0) return
		var node=instantiate(this.monsterPrefab)
		this.groundNode.addChild(node)
		var monster=node.getComponent(Monster)
		this.monsters.push(monster)
		
		monster.setPos(x,y)
		node.setPosition(x,y,0)
	}
}


